//tbTask.controller.ts


import { Inject, Controller, Get, Query, Post, Body, App, All } from "@midwayjs/core";
import { Context } from "@midwayjs/koa";
import { HttpService } from "@midwayjs/axios";
import { Application } from "../interface.js";
import { failres, okres } from "../config/resUtil.js";
import { InjectEntityModel } from "@midwayjs/typeorm";
import { Repository } from "typeorm";
import { tbTaskEntity } from "../entity/tbTask.entity.js";
import { BaseDto } from "../models/Base.dto.js";
import { getSkip } from "../utils/pageUtil.js";


@Controller("/api/tb_task")
export class tbTaskController {
  @Inject()
  ctx: Context;
  @App("koa")
  app: Application;

  @InjectEntityModel(tbTaskEntity, "default")
  tbTaskModel: Repository<tbTaskEntity>;

  //find
  @Post("/getlist")
  async getlist(@Body() body: tbTaskEntity & BaseDto) {
    var query = this.tbTaskModel
      .createQueryBuilder("tb_task").select();
    query = query.where("1=1");


    if (body?.id) {
      query = query.andWhere(`id = :id`, {
        id: body?.id
      });
    }


    if (body?.name) {
      query = query.andWhere(`name = :name`, {
        name: body?.name
      });
    }


    if (body?.content) {
      query = query.andWhere(`content = :content`, {
        content: body?.content
      });
    }


    if (body?.parentId) {
      query = query.andWhere(`parent_id = :parentId`, {
        parentId: body?.parentId
      });
    }

    if (body?.planstarttime) {
      query = query.andWhere(`planstarttime = :planstarttime`,{
        planstarttime:body?.planstarttime
      });
    }



    if (body?.planendtime) {
      query = query.andWhere(`planendtime = :planendtime`,{
        planendtime:body?.planendtime
      });
    }



    if (body?.realstarttime) {
      query = query.andWhere(`realstarttime = :realstarttime`,{
        realstarttime:body?.realstarttime
      });
    }



    if (body?.realendtime) {
      query = query.andWhere(`realendtime = :realendtime`,{
        realendtime:body?.realendtime
      });
    }



    if (body?.state) {
      query = query.andWhere(`state = :state`,{
        state:body?.state
      });
    }






    var result3 = await query.skip(getSkip(body)).take(body.pageSize).getManyAndCount();
    var datalist = (result3?.[0] || []) as unknown as tbTaskEntity[];
    return okres(datalist, { total: result3?.[1] });
  }

  //findone
  @Get("/getbyid")
  async getbyid(@Query("id") id) {
    var query = this.tbTaskModel
      .createQueryBuilder("tb_task").select();
    query = query.where("1=1");
    query = query.andWhere(`id = :id`, { id });
    var result3 = await query.getOne();
    return okres(result3);
  }

  //add
  @Post("/add")
  async add(@Body() body: tbTaskEntity & BaseDto) {
    var result = await this.tbTaskModel.save(body);
    return okres(result);
  }

  //update
  @Post("/update")
  async update(@Body() body: tbTaskEntity & BaseDto) {
    var result = await this.tbTaskModel.update(body?.id, body);
    return okres(result);
  }

  //delbyid
  @Get("/delbyid")
  async delbyid(@Query("id") id) {
    var result = await this.tbTaskModel.delete(id);
    return okres(result);
  }


}
